package com.ttbj.product.zwrefund.zfbpay;

import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeFastpayRefundQueryModel;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.request.AlipayTradeFastpayRefundQueryRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeFastpayRefundQueryResponse;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;


public class AliPayUtil {
	private static final Logger LOGGER = LoggerFactory.getLogger(AliPayUtil.class);   
	private static String APP_ID = ZfbPropertiesUtil.getInstance().getValue("appid");
    //支付宝退款请求的网关
    private static String requestUrl = ZfbPropertiesUtil.getInstance().getValue("open_api_domain");
    //用户自己生成的私钥
    private static String APP_PRIVATE_KEY = ZfbPropertiesUtil.getInstance().getValue("private_key");
    //支付宝公钥
    private static String ALIPAY_PUBLIC_KEY = ZfbPropertiesUtil.getInstance().getValue("alipay_public_key");
    //字符编码
    private static String CHARSET = "UTF-8";
    
    //支付
    public static String aliPay(String money) throws AlipayApiException {
    	AlipayClient alipayClient = new DefaultAlipayClient(requestUrl, APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2");
    	AlipayTradePagePayRequest aliPayRequest = new AlipayTradePagePayRequest();
    	
    	AlipayTradePagePayModel model = new AlipayTradePagePayModel();
    	model.setSubject("nelson-test-title");
    	model.setProductCode("FAST_INSTANT_TRADE_PAY");
    	model.setOutTradeNo(UUID.randomUUID().toString().replace("-", ""));
    	model.setTotalAmount(money);
    	
    	aliPayRequest.setBizModel(model);
    	AlipayTradePagePayResponse response = alipayClient.pageExecute(aliPayRequest);
    	String result = response.getBody();
    	return result;
    }
    
    //支付宝退款操作
    public static AlipayTradeRefundResponse refundOrder(String tradeNo, String refundAmount){
    	//实例化客户端
    	AlipayClient alipayClient = new DefaultAlipayClient(requestUrl, APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2");
    	AlipayTradeRefundModel refundModel = new AlipayTradeRefundModel();
    	refundModel.setTradeNo(tradeNo);
    	refundModel.setRefundAmount(refundAmount);
    	refundModel.setRefundReason("正常退款");
    	
    	AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
    	request.setBizModel(refundModel);
    	AlipayTradeRefundResponse response = null;
    	try {
			response = alipayClient.execute(request);	//真正去请求支付宝进行退款
		} catch (AlipayApiException e) {
			LOGGER.error("支付宝退款错误！",e.getMessage());
		}
    	return response;
    }
    
    //支付宝退款查询操作
    public static AlipayTradeFastpayRefundQueryResponse refundOrerQuery(String tradeNo, String outRequestNo) {
    	AlipayClient alipayClient = new DefaultAlipayClient(requestUrl, APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2");
    	AlipayTradeFastpayRefundQueryModel aQueryModel = new AlipayTradeFastpayRefundQueryModel();
    	aQueryModel.setTradeNo(tradeNo);
    	//请求退款接口时，传入的退款请求号，如果在退款请求时未传入，则该值为创建交易时的外部交易号(out_trade_no)，传入out_trade_no的值
    	aQueryModel.setOutRequestNo(outRequestNo);	
    	
    	AlipayTradeFastpayRefundQueryRequest refundQueryRequest = new AlipayTradeFastpayRefundQueryRequest();
    	refundQueryRequest.setBizModel(aQueryModel);
    	
    	AlipayTradeFastpayRefundQueryResponse aQueryResponse = null;
    	try {
			aQueryResponse = alipayClient.execute(refundQueryRequest);
			LOGGER.info("支付宝退款查询：{}", aQueryResponse.getBody());
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
    	return aQueryResponse;
    }
}
